package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class EditAccountServlet extends HttpServlet {
    private static final String main = "/WEB-INF/jsp/catalog/Main.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        HttpSession session = request.getSession();
        AccountService accountService = new AccountService();
        Account account = (Account) session.getAttribute("account") ;

        String password = request.getParameter("password");
        String repeatedPassword = request.getParameter("repeatedPassword");
        String firstName = request.getParameter("account.firstName");
        String lastName = request.getParameter("account.lastName");
        String email = request.getParameter("account.email");
        String phone = request.getParameter("account.phone");
        String address1 = request.getParameter("account.address1");
        String address2 = request.getParameter("account.address2");
        String city = request.getParameter("account.city");
        String state = request.getParameter("account.state");
        String zip = request.getParameter("account.zip");
        String country = request.getParameter("account.country");
        String languagePerference = request.getParameter("account.languagePreference");
        String favouriteCategoryId = request.getParameter("account.favouriteCategoryId");
        if (password.equals(repeatedPassword)){
            account.setPassword(password);
            account.setFirstName(firstName);
            account.setLastName(lastName);
            account.setEmail(email);
            account.setPhone(phone);
            account.setAddress1(address1);
            account.setAddress2(address2);
            account.setCity(city);
            account.setState(state);
            account.setZip(zip);
            account.setCountry(country);
            account.setLanguagePreference(languagePerference);
            account.setFavouriteCategoryId(favouriteCategoryId);
            account.setListOption(request.getParameter("account.listOption")!= null);
            account.setBannerOption(request.getParameter("account.bannerOption")!=null);
            accountService.updateAccount(account);
            session.setAttribute("account", null);
            request.getRequestDispatcher(main).forward(request,response);

        }
        else {
            session.setAttribute("message","The two passwords are inconsistent");
            request.getRequestDispatcher(ERROR).forward(request,response);
        }

    }
}
